package com.ndp.fb.creative.fetchdata.service.impl;

import com.ndp.fb.creative.fetchdata.dao.domain.Image;
import com.ndp.fb.creative.fetchdata.dao.domain.SumResult;
import com.ndp.fb.creative.fetchdata.dao.mongodb.HashcodeSumMongoService;
import com.ndp.fb.creative.fetchdata.dao.mysql.AccountImageRdbService;
import com.ndp.fb.creative.fetchdata.model.vo.SumResultVo;
import com.ndp.fb.creative.fetchdata.service.AccountImagesService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by evan.wan on 2015/5/21.
 */
@Service
public class AccountImagesServiceImpl implements AccountImagesService{
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    AccountImageRdbService accountImageRdbService;

    @Autowired
    HashcodeSumMongoService hashcodeSumMongoService;

    @Override
    public SumResultVo getHashCodeByName(String name)
    {
        logger.info("AccountImagesServiceImpl getHashCodeByName()进入---》"+ name);
        if(StringUtils.isEmpty(name))
        {
            return null;
        }
        List<Image> results = accountImageRdbService.getHashCodeByName(name);
        SumResultVo vo = new SumResultVo();
        if(results != null && results.size() > 0)
        {
            vo.setName(results.get(0).getName());
            vo.setVisitPath(results.get(0).getVisitPath());
            vo.setLitImgUrl(results.get(0).getLitImgUrl());
            vo.setId(results.get(0).getId());
            vo.setImageHash(results.get(0).getHashCode());
//            List<String> list = new ArrayList<>();
//            list.add(results.get(results.size() - 1).getHashCode());

            List<Long> adGroupIds = accountImageRdbService.getAdGroupIdsByHashCode(results.get(0).getHashCode());

            List<SumResult> sumResults = hashcodeSumMongoService.find(adGroupIds);
            if(sumResults != null && sumResults.size() > 0)
            {
                int clicks=0,impressions=0,loads=0,spent=0;
                for(SumResult sumResult : sumResults)
                {
                    clicks += sumResult.getClicks();
                    impressions += sumResult.getImpressions();
                    loads += sumResult.getResults();
                    spent += sumResult.getSpent();
                }
                vo.setClicks(clicks);
                vo.setImpressions(impressions);
                vo.setResults(loads);
                vo.setSpent(spent);
                vo.setSumResultList(sumResults);
            }
            else
            {
                logger.info("AccountImagesServiceImpl getHashCodeByName() 查询mongdb为空----->hashcode:"+results.get(results.size() - 1));
            }
        }
        return vo;
    }

    @Override
    public List<SumResultVo> getHashCodeByNames(String names)
    {
        logger.info("AccountImagesServiceImpl getHashCodeByNames() enter.");
        List<SumResultVo> vos = new ArrayList<>();
        if(StringUtils.isEmpty(names))
        {
            return null;
        }
        String[] namesArr = names.split(",");
        SumResultVo vo;
        for (int i = 0; i < namesArr.length; i++) {
            vo = getHashCodeByName(namesArr[i]);
            vos.add(vo);
        }
        return vos;
    }

    @Override
    public List<SumResultVo> getHashCodeByHashs(String names)
    {
        logger.info("AccountImagesServiceImpl getHashCodeByHashs() enter.");
        List<SumResultVo> vos = new ArrayList<>();
        if(StringUtils.isEmpty(names))
        {
            return null;
        }
        String[] namesArr = names.split(",");
        SumResultVo vo;
        for (int i = 0; i < namesArr.length; i++) {
            vo = getHashCodeByHash(namesArr[i]);
            vos.add(vo);
        }
        return vos;
    }

    @Override
    public String getHashCodeByFburl(String fbUrl)
    {
        logger.info("AccountImagesServiceImpl getHashCodeByFburl()进入---》"+ fbUrl);
        if(StringUtils.isEmpty(fbUrl))
        {
            return "";
        }
        List<String> results = accountImageRdbService.getHashCodeByFburl(fbUrl);
        if(results != null && results.size() > 0)
        {
            return results.get(results.size() - 1);

        }
        return "";
    }

    private SumResultVo getHashCodeByHash(String hash)
    {
        logger.info("AccountImagesServiceImpl getHashCodeByHash()进入---》"+ hash);
        if(StringUtils.isEmpty(hash))
        {
            return null;
        }
        List<Image> results = accountImageRdbService.getHashCodeByHash(hash);

        //根据hash查询到几个图片名字
        List<String> nameS = accountImageRdbService.getNamesByHash(hash);
        SumResultVo vo = new SumResultVo();
        if(results != null && results.size() > 0 && nameS != null && nameS.size() > 0)
        {
            StringBuffer namestr = new StringBuffer("找到"+nameS.size()+"个素材        ");
            for(int m=0;m<nameS.size();m++)
            {
                namestr.append(nameS.get(m));
                if(m < nameS.size() -1)
                    namestr.append("、");
            }
            vo.setName(namestr.toString());
            vo.setVisitPath(results.get(0).getVisitPath());
            vo.setLitImgUrl(results.get(0).getLitImgUrl());
            vo.setId(results.get(0).getId());
            vo.setImageHash(results.get(0).getHashCode());
//            List<String> list = new ArrayList<>();
//            list.add(results.get(results.size() - 1).getHashCode());

            List<Long> adGroupIds = accountImageRdbService.getAdGroupIdsByHashCode(hash);

            List<SumResult> sumResults = hashcodeSumMongoService.find(adGroupIds);
            if(sumResults != null && sumResults.size() > 0)
            {
                int clicks=0,impressions=0,loads=0,spent=0;
                for(SumResult sumResult : sumResults)
                {
                    clicks += sumResult.getClicks();
                    impressions += sumResult.getImpressions();
                    loads += sumResult.getResults();
                    spent += sumResult.getSpent();
                }
                vo.setClicks(clicks);
                vo.setImpressions(impressions);
                vo.setResults(loads);
                vo.setSpent(spent);
                vo.setSumResultList(sumResults);
            }
            else
            {
                logger.info("AccountImagesServiceImpl getHashCodeByHash() 查询mongdb为空----->hashcode:"+results.get(results.size() - 1));
            }
        }
        return vo;
    }

}
